Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic_modules: HTTP filter config implementation #37070

Merged
merged 16 commits into from
Nov 21, 2024

Conversation

mathetake
Copy link
Member

@mathetake mathetake commented Nov 8, 2024

Commit Message: dynamic_modules: HTTP filter config implementation
Additional Description:

This expands the ABI for HTTP filter configurations. Especially this adds two
event hooks coupled with the life cycle of HTTP filter config handled in the main
thread.

The key idea is to do the direct pointer (context) passing between the boundary;
This allows us to avoid maintaining IDs and global mapping state, which makes it
easier to test as well as it has benefit in terms of performance. E.g. there's no
need to look up "contexts" on each event hook entry.

The next follow-up PR will add per-stream event hooks (filter implementation).
After the event hooks are done, module->Envoy functions will be added (e.g.
accessing headers, etc.)

Risk Level: low
Testing: done
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #37070 was opened by mathetake.

see: more, trace.

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake mathetake marked this pull request as ready for review November 8, 2024 23:10
@mathetake
Copy link
Member Author

cc @marc-barry

@mathetake
Copy link
Member Author

sorry for the slow progress for the last few weeks - i've been swamped by other works, now I managed to allocate some cycles on this one. The plan is after this PR, the per-stream event hooks will be implemented and then module->envoy call backs (e.g. getting headers etc) will be added. Feels like we will soon be able to provide basic functionality.

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake
Copy link
Member Author

/retest

@mathetake
Copy link
Member Author

As a side note, Linkedin's infra team has extensively tested my PoC impl, and according to their evaluation, it's working pretty great and they observed almost zero overhead compared with the native C++ impl, which sounds awesome. The work here is and subsequent PRs will be based on their feedback as well as the original PoC implementation.

Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/wait

Comment on lines +50 to +51
typedef const void* // NOLINT(modernize-use-using)
envoy_dynamic_module_type_http_filter_config_envoy_ptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the purpose of passing void* here if the user has to have the actual C++ definition. Is this just to avoid any C++ leaking into the ABI itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid question. The users (modules) do not need to understand the C++ definition, but instead users can just call the corresponding ABI functions with this opaque pointers. For example, one notable example about filter-config level ABI function is "creating metrics". Then possible function signature for that ABI would look like

envoy_dynamic_module_type_counter_metric_envoy_ptr envoy_dynamic_module_define_counter_metric(
    envoy_dynamic_module_type_http_filter_config_envoy_ptr envoy_filter_config_ptr,
    const char* name,
    int name_length,
);

where the module->Envoy ABI function has this opaque pointer as a first argument. Then on the Envoy side, we can provide the functionality

envoy_dynamic_module_type_counter_metric_envoy_ptr envoy_dynamic_module_define_counter_metric(
    envoy_dynamic_module_type_http_filter_config_envoy_ptr envoy_filter_config_ptr,
    const char* name,
    int name_length,
) {
  const std::string_view name_view(static_cast<const char*>(name), name_length);
  auto scope = static_cast<DynamicModuleHttpFilterConfig*>(envoy_filter_config_ptr)->stat_scope_;
  Stats::StatNameManagedStorage storage(name_view, scope->symbolTable());
  Stats::StatName stat_name = storage.statName();
  Stats::Counter& counter = scope->counterFromStatName(stat_name);
  return &counter;
}

without leaking the implementation details of this envoy side "HttpFilterConfig" class into dynamic modules.

Apologies this is difficult to see the rationale behind this pointer passing without the concrete impl example like this. Later when I add these ABI functions, then we will be able to see the concrete picture more clearly. Hope this helps!

/// This is useful to perform any process-wide initialization that the dynamic module needs.
/// The first argument has [`ProgramInitFunction`] type, and it is called when the dynamic module is loaded.
///
/// The second argument has [`NewHttpFilterConfigFunction`] type, and it is called when the new HTTP filter configuration is created.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you setup in some future PR rustfmt and make sure it runs in CI? This config https://github.com/bitdriftlabs/shared-core/blob/main/rustfmt.toml will make it mostly look like Envoy code style. For now you can run it manually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, it's already enabled in this PR with the default config

# As per the discussion in https://github.com/envoyproxy/envoy/pull/35627,
# we set the rust_fmt and clippy target here instead of the part of //tools/code_format target for now.
rustfmt_test(
name = "rust_sdk_fmt",
tags = ["nocoverage"],
targets = ["//source/extensions/dynamic_modules/sdk/rust:envoy_proxy_dynamic_modules_rust_sdk"],
)
rust_clippy(
name = "rust_sdk_clippy",
tags = ["nocoverage"],
deps = ["//source/extensions/dynamic_modules/sdk/rust:envoy_proxy_dynamic_modules_rust_sdk"],
)

so i will follow up with the TOML config after this

source/extensions/dynamic_modules/sdk/rust/src/lib.rs Outdated Show resolved Hide resolved
source/extensions/dynamic_modules/sdk/rust/src/lib.rs Outdated Show resolved Hide resolved
and add the unit tests around them. In addition, this replaces global static mut with LazyLock

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake
Copy link
Member Author

i am surprised that ASAN works for Rust SDK tests as well (pure Rust codes) without any build tweaks, feeling good

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake
Copy link
Member Author

mathetake commented Nov 18, 2024

looks like tsan failure is more generic like Rust level known issue: rust-lang/rust#39699 basically std::sync::* family is using fences which are not well supported well by TSAN in general, though ASAN seems working

looks like the exact issue is rust-lang/rust#39608 as I verified that empty .rs file with empty tests fails with sanitizer (meaning test harness itself is being detected by TSAN)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake
Copy link
Member Author

/retest

@mattklein123 mattklein123 merged commit 75e54af into envoyproxy:main Nov 21, 2024
24 checks passed
@mathetake mathetake deleted the httpfactoryabi branch November 21, 2024 16:40
mattklein123 pushed a commit that referenced this pull request Nov 22, 2024
Commit Message: dynamic_modules: enables rustfmt.toml
Additional Description:

This enables the root configuration for rustfmt as a follow up
on
#37070 (comment).
Only formatting is done in this commit. There's no change in its code.

Risk Level: low
Testing: n/a 
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants